package com.doubao.doubao.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doubao.doubao.common.api.ApiResult;
import com.doubao.doubao.mapper.BmsApplyADMapper;
import com.doubao.doubao.mapper.BmsReFilesMapper;
import com.doubao.doubao.model.entity.BmsApply;
import com.doubao.doubao.model.entity.BmsGroupTrue;
import com.doubao.doubao.model.entity.BmsReFiles;
import com.doubao.doubao.model.entity.UmsUser;
import com.doubao.doubao.service.IBmsApplyADService;
import com.doubao.doubao.service.IBmsReFilesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/Apply")
public class BmsApplyADController extends BaseController {

    @Resource
    private IBmsApplyADService iBmsApplyADService;
    @Resource
    BmsApplyADMapper bmsApplyADMapper;

    @GetMapping("/getAllApply")
    public ApiResult<List<BmsApply>> list() {
        List<BmsApply> list = iBmsApplyADService.list();
        return ApiResult.success(list);
    }


    @PostMapping("/addApplyAD")
    public ApiResult<BmsApply> addApply(@RequestBody BmsApply bmsApply) {

        BmsApply res =  bmsApplyADMapper.selectOne(Wrappers.<BmsApply>lambdaQuery()
                .eq(BmsApply::getUserId,bmsApply.getUserId())
                .eq(BmsApply::getRole,2)
        );
        BmsApply res1 =  bmsApplyADMapper.selectOne(Wrappers.<BmsApply>lambdaQuery()
                .eq(BmsApply::getUserId,bmsApply.getUserId())
                .eq(BmsApply::getRole,1)
                .eq(BmsApply::getStatus, bmsApply.getStatus())
        );
        BmsApply res2 =  bmsApplyADMapper.selectOne(Wrappers.<BmsApply>lambdaQuery()
                .eq(BmsApply::getUserId,bmsApply.getUserId())
                .eq(BmsApply::getRole,3)
//                .eq(BmsApply::isStatus, false)
        );

        if(res!=null){
            return ApiResult.failed("你已经发送申请了，请不要重复申请");
        }
        if(res1!=null &&res2==null){
            return ApiResult.failed("你已经是管理员了，请不要重复申请");
        }
        if(res2!=null && res1!=null){
            return ApiResult.failed("你已无资格申请了");
        }

        iBmsApplyADService.save(bmsApply);
        return ApiResult.success(bmsApply);
    }


    @PostMapping("/updateApply")
    public ApiResult<BmsApply> updateApply(@RequestBody  BmsApply bmsApply) {
        iBmsApplyADService.updateById(bmsApply);
        return ApiResult.success(bmsApply);
    }



}
